package com.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chat.domain.pojo.Group;
import com.chat.domain.pojo.GroupMember;
import com.chat.domain.pojo.User;
import com.chat.mapper.GroupMapper;
import com.chat.mapper.GroupMemberMapper;
import com.chat.service.GroupService;
import com.chat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @author wb
 * @since 2025-06-06
 */

@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {

    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Autowired
    private UserService userService;

    @Override
    public Group getById(Serializable id) {
        Group group = super.getById(id);
        List<User> list = userService.getUsersByGroupId(group.getId());
        group.setMembers(list);

        return group;
    }

    @Cacheable(value = "groups", keyGenerator = "keyGenerator")
    @Override
    public List<Group> getGroupsByUserId(Integer userId) {
        return baseMapper.selectGroupsByUserId(userId);
    }

    @CacheEvict(value = "groups", allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean create(String name, String desc, String remark, Integer userId) {
        Group group = new Group();
        group.setName(name);
        group.setDescription(desc);
        group.setRemark(remark);
        group.setCreateBy(userId);

        Boolean b = super.save(group);

        if (b) {
            GroupMember member = new GroupMember();
            member.setGroupId(group.getId());
            member.setUserId(userId);

            groupMemberMapper.insert(member);
        }

        return b;
    }

    @CacheEvict(value = "groups", allEntries = true)
    @Override
    public boolean removeById(Group entity) {
        return super.removeById(entity);
    }

    @CacheEvict(value = "groups", allEntries = true)
    @Override
    public Boolean quit(Integer groupId, Integer userId) {
        LambdaQueryWrapper<GroupMember> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GroupMember::getGroupId, groupId);
        lqw.eq(GroupMember::getUserId, userId);

        return groupMemberMapper.delete(lqw) > 0;
    }

    @Override
    public Boolean addMember(String chatId, Integer friendId) {
        Integer groupId = Integer.parseInt(chatId.substring(chatId.lastIndexOf("-") + 1));
        GroupMember groupMember = new GroupMember();
        groupMember.setGroupId(groupId);
        groupMember.setUserId(friendId);

        return groupMemberMapper.insert(groupMember) > 0;
    }

    @Override
    public List<Integer> getMembers(Integer groupId) {
        return groupMemberMapper.selectObjs(new LambdaQueryWrapper<GroupMember>()
                .eq(GroupMember::getGroupId, groupId)
                .select(GroupMember::getUserId)
        );
    }
}
